package com.leo.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pc
 */
public class PoUtils {

    private static Logger logger = LoggerFactory.getLogger(PoUtils.class);

    public static <DO, PO> DO copyBean(PO po, Class<DO> dataObjectClass) {
        DO d = null;
        try {
            d =dataObjectClass.newInstance();
            BeanUtils.copyProperties(po, d);
        } catch (InstantiationException e) {
            logger.error("不能创建DataObject class的实例！class is: {}", dataObjectClass, e);
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            logger.error("class{}的实例访问权限不足，{} ", dataObjectClass,e.getMessage(), e);
            e.printStackTrace();
        }finally {
            logger.info("复制属性结束！！！");
        }
        return d;
    }

    public static <DO, PO> List<DO> copyBeans(List<PO> list, Class<DO> dataObjectClass) {
        if (list == null) {
            return null;
        }
        List<DO> rets = new ArrayList<DO>(list.size());
        DO b;
        for (PO po : list) {
            b = copyBean(po, dataObjectClass);
            if(b!=null) {
                rets.add(b);
            }
        }
        return rets;
    }
}
